﻿using D3StatsClient.Model;
using D3StatsClient.ViewModel;
using DBLootTrackerPlugin.ServiceModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace D3StatsClient.ServiceModel
{
    public class LootTrackerDataServiceCallbacks : ILootTrackerDataServiceCallbacks
    {
        public void MyCallbackFunction(D3TrackerServiceItem value)
        {
            if (value == null)
                return;

            var vm = ViewModelManager<MainWindowViewModel>.ViewModel;

            //log gold to the gold counter.
            if (value.Name == "Gold")
            {
                vm.Stats.Gold += value.GoldAmount;
                return;
            }
               

            if (this.IsTrackable(value))
            {

                D3TrackerItem item = new D3TrackerItem();
                item.ItemLevel = value.Level;
                item.Name = value.Name;
                item.Quality = string.IsNullOrWhiteSpace(value.Quality) ? ItemQuality.Invalid : (ItemQuality)Enum.Parse(typeof(ItemQuality), value.Quality);
                item.MagicFind = value.MagicFind;
                item.Time = value.TimeStamp;
                item.Location = value.DropLocationName;
                item.ItemLink = value.ItemLink;
                item.Server = vm.Server;
                item.ID = value.ID;
                item.IsPickedUp = value.IsPickedUp;

                if (!vm.provider.ContainsDrop(value.ID))
                {
                    
                    item.Save(vm.provider);
                }
                else
                {
                    item.Update(vm.provider);
                }
            }

            
        }

        private bool IsTrackable (D3TrackerServiceItem value)
        {
            List<string> ignoreList = D3StatsUtility.DelimitedStringToList(Properties.Settings.Default.IgnoreList);

            var result = (from l in ignoreList where l.ToLower() == value.Name.ToLower() select l).FirstOrDefault();

            if(!string.IsNullOrWhiteSpace(result))
            {
                return false;
            }

            return true;
        }
    }
}
